const Evt = require("./event.js")
const Utils = require("./utils.js")
const {EN} = require("./constant.js")
module.exports = 
class Shape extends Evt{
	constructor($env,$event){
		super();
		this.$env = $env;
		this.$event = $event;
		this.ctx=$env.ctx;
		this.cvs=($env.ctx||{}).canvas;
		this.baseContext = this.$env.baseContext;
		
		this.isActive = true;
		this.__relativePlugins = [];
		this.__steps = [];
		this.__setting = {
			inPath:false,
			x:0,
			y:0,
			r:4,
			s:0,
			e:Math.PI*2,
			at:false
		}
		//像素与展示msg的比例
		this.ratio = 50
		this.fixed = 2

		//false 不做 is in path 判断
		this.__isInPath = true;
		//label所用
		this.__letters = ""
		this.__t="'''''''''"
		this.__ghostCvs = Utils.createDOM("canvas",{
			id:"ghost-cvs-in-ram"
		})
	}
	draw(){}
	ghostDraw(){}
	__ID(){
		return Math.random().toString().replace(/\./,"")
	}
	generateLabel(){
		let index = this.$env[this.name];
		let m = index%this.__letters.length
		let t = Math.floor(index/this.__letters.length)
		return this.__letters[m]+this.__t.slice(0,t)
	}
	cvsEvtPos(e,cvs){
		let rect = cvs.getBoundingClientRect();
		return {
			x:e.x-rect.left-document.documentElement.scrollLeft,
			y:e.y-rect.top-document.documentElement.scrollTop
		}
	}
	clearBaseContext(){
		let bctx = this.baseContext;
		bctx&&bctx.clearRect(0,0,bctx.canvas.width,bctx.canvas.height);
	}
	drawPoint(p,context){
		let ctx = context||this.ctx;
		if(p.inPath)return
		ctx.save()
		ctx.lineWidth=1
		ctx.strokeStyle="#000";
		ctx.fillStyle = p.strokeStyle;
		ctx.setLineDash([0])
		ctx.beginPath()
		ctx.arc(
			p.x,
			p.y,
			p.r,
			p.s,
			p.e,
			p.at)
		ctx.fill();
		ctx.stroke();
		ctx.restore()
	}
	drawLine(ps,context){
		let ctx = context||this.ctx;
		ctx.beginPath()
		ctx.moveTo(ps[0].x,ps[0].y)
		ctx.lineTo(ps[1].x,ps[1].y)
		ctx.closePath()
		ctx.stroke();
	}
	radius(fp,lp,ratio=1){
		let dx = fp.x-lp.x;
		let dy = fp.y-lp.y;
		let res = Math.pow((dx*dx+dy*dy),.5)/ratio
		return res.toFixed(this.fixed);
	}
	rect(fp,lp,ratio=1){
		let dx = Math.abs(fp.x-lp.x);
		let dy = Math.abs(fp.y-lp.y);
		return {
			hr:(dx/ratio).toFixed(this.fixed),
			zr:(dy/ratio).toFixed(this.fixed)
		}
	}
	tipHide(){
		this.$event.emit("tip:hide")
	}
	documentRect(){
		return { 
			w : document.documentElement.clientWidth,
			h: document.documentElement.clientHeight
		}
	}
	isInPath(px,py){

		let {w,h} = this.documentRect()
		this.__ghostCvs.width=w;
		this.__ghostCvs.height=h;
		let ctx = this.__ghostCvs.getContext("2d")
		let arr = []
		this.$env.__steps.filter((s)=>{
			return s.cmp.__isInPath
		}).forEach((s)=>{
			arr = arr.concat(s.data)
		})
		ctx.clearRect(0,0,w,h);
		return arr.filter((p)=>{
			if(isNaN(p)){
				let {x,y} = p
				this.drawPoint(Utils.assign(this.__setting,{x,y}),ctx)
				return ctx.isPointInPath(px,py)
			}else return false
		})
	}
	rewritePos(pos){
		let ips = this.isInPath(pos.x,pos.y)
		if(ips.length===0)return pos
		let p = ips.sort((fp,sp)=>{
			return this.radius(pos,fp)-this.radius(pos,sp)
		})[0]
		pos.x=p.x
		pos.y=p.y
		pos.inPath = true
		return pos;
	}
}
